package pl.edu.pw.perg.compinfodb.dbexport;

import java.io.IOException;
import java.util.List;

import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import pl.edu.pw.perg.compinfodb.entities.ElementType;
import pl.edu.pw.perg.compinfodb.manager.IConnectorManager;
import pl.edu.pw.perg.compinfodb.manager.IConnectorNameManager;
import pl.edu.pw.perg.compinfodb.manager.IElementTypeManager;
import pl.edu.pw.perg.compinfodb.manager.IFieldManager;
import pl.edu.pw.perg.compinfodb.manager.IHistoryManager;
import pl.edu.pw.perg.compinfodb.manager.IPhysicalElementManager;
import pl.edu.pw.perg.compinfodb.manager.IVirtualElementManager;
import pl.edu.pw.perg.compinfodb.xls.to.XLSMetadataWriter;

public class DBExporter extends XLSMetadataWriter {

	private IFieldManager fieldManager;
	private IElementTypeManager typeManager;
	private IVirtualElementManager virtualElementManager;
	private IPhysicalElementManager physicalElementManager;
	private IConnectorManager connectorManager;
	private IConnectorNameManager connectorNameManager;
	private IHistoryManager historyManager;
	private static Logger logger = LoggerFactory.getLogger(DBExporter.class);

	public DBExporter() {
		String dbCtxPath = "classpath:pl/edu/pw/perg/compinfodb/dbexport/ctx/dbexport-context.xml";
		FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext(
				dbCtxPath);
		fieldManager = (IFieldManager) ctx.getBean("fieldManager");
		typeManager = (IElementTypeManager) ctx.getBean("typeManager");
		virtualElementManager = (IVirtualElementManager) ctx
				.getBean("virtualElementManager");
		physicalElementManager = (IPhysicalElementManager) ctx
				.getBean("physicalElementManager");
		connectorManager = (IConnectorManager) ctx.getBean("connectorManager");
		connectorNameManager = (IConnectorNameManager) ctx.getBean("connectorNameManager");
		historyManager = (IHistoryManager) ctx.getBean("historyManager");

	}

	public void write() {
		logger.info("Exporting data from DB to xls, please wait...");
		try {
			logger.info("Exporting data:");
			logger.info("* Connections");
			setConnections(connectorManager.findAll());
			logger.info("* Connector Names");
			setConnectorNames(connectorNameManager.findAll());
			logger.info("* Fields");
			setFields(fieldManager.findAll());
			logger.info("* History");
			setHistory(historyManager.findAll());
			logger.info("* Types");
			List<ElementType> types = typeManager.findAll();
			// setInternalConnections(types);
			setTypes(types);
			logger.info("* Modules");
			setModules(physicalElementManager.findAll());
			logger.info("* Structure");
			setStructure(virtualElementManager.findAll());
			logger.info("Saving...");
			save();
			logger.info("All data exported.");
		} catch (RowsExceededException e) {
			logger.error("Rows exceeded", e);
			System.exit(1);
		} catch (WriteException e) {
			logger.error("Cannot write xls file during save", e);
			System.exit(1);
		} catch (IOException e) {
			logger.error("IO exception", e);
			System.exit(1);
		}

	}

	public static void main(String[] args) {
		try {
			DBExporter xls = new DBExporter();
			xls.write();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	public void setFieldManager(IFieldManager fieldManager) {
		this.fieldManager = fieldManager;
	}

	public void setTypeManager(IElementTypeManager typeManager) {
		this.typeManager = typeManager;
	}

	public void setVirtualElementManager(
			IVirtualElementManager virtualElementManager) {
		this.virtualElementManager = virtualElementManager;
	}

	public void setPhysicalElementManager(
			IPhysicalElementManager physicalElementManager) {
		this.physicalElementManager = physicalElementManager;
	}

	public void setConnectorManager(IConnectorManager connectorManager) {
		this.connectorManager = connectorManager;
	}

	public void setHistoryManager(IHistoryManager historyManager) {
		this.historyManager = historyManager;
	}

	public void setConnectorNameManager(IConnectorNameManager connectorNameManager) {
		this.connectorNameManager = connectorNameManager;
	}

}
